﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 异步信号量
    /// </summary>
    public class AsyncSemaphore
    {
        //字段
        private static readonly Task Completed = Task.FromResult(true);
        private readonly Queue<TaskCompletionSource<bool>> _waiters = new ();
        private int _currentCount;

        /// <summary>
        /// 初始化一个异步信号量（<see cref="AsyncSemaphore"/>）的新实例
        /// </summary>
        /// <param name="initialCount"></param>
        public AsyncSemaphore(int initialCount)
        {
            initialCount.CheckGreaterThan(nameof(initialCount), 0);
            _currentCount = initialCount;
        }

        /// <summary>
        /// 异步等待
        /// </summary>
        public Task WaitAsync()
        {
            lock (_waiters)
            {
                if (_currentCount > 0)
                {
                    --_currentCount;
                    return Completed;
                }
                var waiter = new TaskCompletionSource<bool>();
                _waiters.Enqueue(waiter);
                return waiter.Task;
            }
        }
        
        /// <summary>
        /// 释放
        /// </summary>
        public void Release()
        {
            TaskCompletionSource<bool> toRelease = null;
            lock (_waiters)
            {
                if (_waiters.Count > 0)
                {
                    toRelease = _waiters.Dequeue();
                }
                else
                {
                    ++_currentCount;
                }
            }
            toRelease?.SetResult(true);
        }

    }
}